﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;

using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmContractOrder : BaseForm
    {
        public RoomInfo RoomInfo = null;

        public FrmContractOrder()
        {
            InitializeComponent();
        }

        private void FrmContractOrder_Load(object sender, EventArgs e)
        {
            this.txtSex.SelectedIndex = 0;
            this.txtStatus.SelectedIndex = 0;

            this.txtIDCardType.Items.Clear();
            this.txtIDCardType.Items.AddRange(DictItemUtil.GetIDCarType());
            if (this.txtIDCardType.Items.Count > 0)
            {
                this.txtIDCardType.SelectedIndex = 0;
            }
            this.txtName.Focus();

            if (RoomInfo != null)
            {
                this.txtRoomNo.Text = RoomInfo.RoomNo;
            }
        }
        private void btnNameSearch_Click(object sender, EventArgs e)
        {
            FrmCustomerSearch dlg = new FrmCustomerSearch();
            dlg.txtName.Text = this.txtName.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(dlg.SelectedID))
                {
                    ChangeCustomer(dlg.SelectedID);
                }
            }
        }

        private void btnIDSearch_Click(object sender, EventArgs e)
        {
            FrmCustomerSearch dlg = new FrmCustomerSearch();
            dlg.txtName.Text = this.txtIDNumber.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(dlg.SelectedID))
                {
                    ChangeCustomer(dlg.SelectedID);
                }
            }
        }

        private void btnTelSearch_Click(object sender, EventArgs e)
        {
            FrmCustomerSearch dlg = new FrmCustomerSearch();
            dlg.txtName.Text = this.txtTelephone.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(dlg.SelectedID))
                {
                    ChangeCustomer(dlg.SelectedID);
                }
            }
        }

        private void ChangeCustomer(string customerID)
        {
            if (!string.IsNullOrEmpty(customerID))
            {
                CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(customerID);
                if (info != null)
                {
                    this.txtAddress.Text = info.Address;
                    this.txtIDCardType.Text = info.IDCarType;
                    this.txtIDNumber.Text = info.IDNumber;
                    this.txtName.Text = info.Name;
                    this.txtSex.Text = info.Sex;
                    this.txtTelephone.Text = info.Telephone;
                }
            }
        }

        private bool AddCustomerToDB()
        {
            CustomerInfo info = new CustomerInfo();
            info.Name = this.txtName.Text.Trim();
            info.Address = this.txtAddress.Text;
            info.IDCarType = this.txtIDCardType.Text;
            info.IDNumber = this.txtIDNumber.Text;
            info.Note = this.txtNote.Text;
            info.RegistrationDate = DateTime.Now;
            info.Sex = this.txtSex.Text;
            info.Telephone = this.txtTelephone.Text;

            return BLLFactory<Customer>.Instance.Insert(info);
        }        

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 输入检查

            this.DialogResult = DialogResult.None;
            if (this.txtName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入主客姓名");
                this.txtName.Focus();
                return;
            }
            else if (this.txtIDNumber.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入证件编码");
                this.txtIDNumber.Focus();
                return;
            }

            string userName = this.txtName.Text.Trim();
            CustomerInfo existInfo = BLLFactory<Customer>.Instance.CheckUser(userName, this.txtIDNumber.Text);
            if (existInfo == null)
            {
                #region 确保数据库中存在指定的记录
                string message = string.Format("宾客信息在数据库中不存在，需要添加到数据库中才能下订单。\r\n您是否确定增加到数据库中？");
                if (MessageUtil.ShowYesNoAndTips(message) == DialogResult.No)
                {
                    return;
                }
                else
                {
                    try
                    {
                        bool success = AddCustomerToDB();
                        if (!success)
                        {
                            MessageUtil.ShowWarning("客户信息添加未成功");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error("客户信息添加出错", ex);
                        MessageUtil.ShowWarning("客户信息添加出错");
                        return;
                    }
                }
                #endregion
            }

            #endregion

            #region 保存订单
            //万事俱备，只欠东风
            existInfo = BLLFactory<Customer>.Instance.CheckUser(userName, this.txtIDNumber.Text);
            if (existInfo != null)
            {
                try
                {
                    #region 构建订单对象
                    OrderInfo orderInfo = new OrderInfo();
                    orderInfo.CreateDate = DateTime.Now;
                    orderInfo.Createor = Portal.gc.LoginInfo.FullName;
                    orderInfo.Customer_ID = existInfo.ID;
                    orderInfo.CustomerName = existInfo.Name;
                    orderInfo.IDNumber = existInfo.IDNumber;
                    orderInfo.Note = this.txtNote.Text;
                    orderInfo.MasterRoomNo = RoomInfo.RoomNo;
                    orderInfo.PayType = "现金";
                    orderInfo.IsCheckOut = false;
                    orderInfo.DepositMoney = Convert.ToDecimal(this.txtDeposit.Text);
                    orderInfo.RoomType = RoomInfo.RoomType;
                    orderInfo.RoomNo = RoomInfo.RoomNo;
                    orderInfo.ContractPreOff = this.txtPreOffTime.Value;
                    orderInfo.ContractPrice = Convert.ToDecimal(this.txtUnitCost.Text);
                    orderInfo.ContractStatus = this.txtStatus.Text;
                    orderInfo.ContractUnit = this.txtUnitName.Text;
                    orderInfo.IsContractOrder = true;
                    //orderInfo.IsPartTime = false;
                    //orderInfo.OrderNo = this.txtOrderNo.Text;
                    //orderInfo.Persons = Convert.ToInt32(this.txtPersons.Text);
                    //orderInfo.PreLivedDays = Convert.ToInt32(this.txtPrLivedDays.Text);
                    //orderInfo.DiscountPrice = Convert.ToDecimal(this.txtDicountPrice.Text);
                    //orderInfo.DiscountRate = Convert.ToDouble(this.txtDiscountRate.Text); 
                    #endregion

                    int orderID = BLLFactory<Order>.Instance.Insert2(orderInfo);
                    InitConsumer(orderID, orderInfo.RoomType, orderInfo.RoomNo);
                    BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.住净房.ToString());

                    MessageUtil.ShowTips("保存订单成功");
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError("保存订单出错：" + ex.Message);
                }
            }
            #endregion
        }

        private void InitConsumer(int orderID, string roomType, string roomNo)
        {
            if (orderID > 0)
            {
                List<InitConsumerInfo> consumerList = BLLFactory<InitConsumer>.Instance.FindByRoomType(roomType);
                foreach (InitConsumerInfo initInfo in consumerList)
                {
                    ConsumerListInfo info = new ConsumerListInfo();
                    info.Amount = initInfo.Amount;
                    info.BeginTime = DateTime.Now;
                    info.Creator = Portal.gc.LoginInfo.FullName;
                    info.Discount = initInfo.Discount;
                    info.DiscountPrice = initInfo.DiscountPrice;
                    info.EndTime = DateTime.Now;
                    info.ItemName = initInfo.ItemName;
                    info.ItemType = initInfo.ItemType;
                    info.ItemNo = initInfo.ItemNo;
                    info.Order_ID = orderID;
                    info.Price = initInfo.Price;
                    info.Quantity = initInfo.Quantity;
                    info.RoomNo = roomNo;
                    info.Waiter = "*";

                    bool success =BLLFactory<ConsumerList>.Instance.Insert(info);
                    if (success)
                    {
                        BLLFactory<Goods>.Instance.AddStockQuantiy(info.ItemNo, info.ItemName, -Convert.ToInt32(info.Quantity));
                    }
                }
            }
        }
    }
}
